package com.tshopping.service.impl;

import com.tshopping.bean.Goods;
import com.tshopping.bean.GoodsCollection;
import com.tshopping.bean.Page;
import com.tshopping.dao.GoodsCollectionDao;
import com.tshopping.dao.GoodsDao;
import com.tshopping.service.GoodsCollectionService;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * @name: GoodsCollectionServiceImpl
 * @description: 收藏商品业务层类
 * @author: Jackeys
 * @date: 2021-01-29 15:28
 */
@Service(value = "goodsCollectionService")
@Transactional
public class GoodsCollectionServiceImpl implements GoodsCollectionService {

    @Autowired
    private GoodsCollectionDao goodsCollectionDao;

    @Autowired
    private GoodsDao goodsDao;
    @Override
    /**
     * @description 处理用户收藏商品
     * @Param: user
     * @Param: goods
     * @Param: time
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 15:31
     */
    public ResponseData addGoodsCollection(int user, int goods, String time) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",goodsCollectionDao.insertGoodsCollection(user,goods,time));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户取消收藏商品
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 15:31
     */
    public ResponseData deleteGoodsCollectionById(int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",goodsCollectionDao.deleteGoodsCollectionById(id));
        return responseData;
    }

    @Override
    public ResponseData selectGoodsCollectionList(int user, int currentPage) {
        ResponseData responseData=ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 10;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = goodsCollectionDao.selectCountByUserId(user);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        ArrayList<GoodsCollection> goodsCollections=goodsCollectionDao.selectGoodsCollectionsByUserId(user,start,size);
        ArrayList<Goods> goodsList=new ArrayList<>();
        for(GoodsCollection goodsCollection:goodsCollections){
            goodsList.add(goodsDao.selectGoodsByGoodsId(goodsCollection.getGoods()));
        }
        responseData.putDataValue("goodsList",goodsList);
        responseData.putDataValue("page", page);
        return responseData;
    }

    @Override
    /**
     * @description 处理判断用户是否收藏该商品
     * @Param: goods
     * @Param: user
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 15:35
     */
    public ResponseData selectCount(int goods, int user) {
        ResponseData responseData=ResponseData.ok();
        boolean flag;
        if(goodsCollectionDao.selectCountByGoodsAndUser(goods,user)==1){
            flag=true;
        }else{
            flag=false;
        }
        responseData.putDataValue("flag",flag);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户取消商品收藏
     * @Param: user
     * @Param: goods
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-29 16:29
     */
    public ResponseData deleteGoodsCollectionByUserAndGoods(int user, int goods) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",goodsCollectionDao.deleteGoodsCollectionByUserAndGoods(user,goods));
        return responseData;
    }
}
